<template>
  <div>
    <div class="login"></div>

    <!--登录中间块-->
    <div class="login-mid">
      <div class="login-mid-top">
        <div class="shadow-top-left"></div>
        <div class="shadow-top-right"></div>
      </div>
      <div class="login-mid-mid">
        <!--捕获人脸区域-->
        <div class="videoCamera-canvasCamera">
          <video
            id="videoCamera"
            :width="videoWidth"
            :height="videoHeight"
            autoplay
          ></video>
          <canvas
            style="display: none"
            id="canvasCamera"
            :width="videoWidth"
            :height="videoHeight"
          ></canvas>

          <!--人脸特效区域-->
          <div v-if="faceImgState" class="face-special-effects-2"></div>
          <div v-else class="face-special-effects"></div>
        </div>

        <!--按钮区域-->
        <div class="face-btn">
          <button @click="faceVef()">
            {{ faceImgState ? "正在识别中..." : "开始识别" }}
          </button>
        </div>

        <!--消息区域-->
        <div class="msg">
          <div class="server-msg">{{ msg }}</div>
          <div class="welcome">人 脸 识 别</div>
          <div class="face-btn" style="margin-top: -2px">
            <button @click="back()">返回</button>
          </div>
        </div>
      </div>
      <div class="login-mid-bot">
        <div class="shadow-bot-left"></div>
        <div class="shadow-bot-right"></div>
      </div>
    </div>
  </div>
</template>
<script>
import $camera from "./index.js";
import Cookie from "js-cookie";
export default {
  data() {
    return {
      videoWidth: 200,
      videoHeight: 200,
      msg: "",
      faceImgState: false,
      faceOption: {},
      redirectUrl:null,
    };
  },
  mounted() {
    //调用摄像头
    this.faceOption = $camera.getCamera({
      videoWidth: 200,
      videoHeight: 200,
      thisCancas: null,
      thisContext: null,
      thisVideo: null,
      canvasId: "canvasCamera",
      videoId: "videoCamera",
    });

    //this.getCompetence();
  },
  methods: {
    // 调用后台接口
    faceVef() {
      let that = this
      // 开始绘制图片
      let imageBase = $camera.draw(this.faceOption);
      const formData = new FormData();
      formData.append("file", imageBase, "人脸登录照片");

      if (imageBase === "" || imageBase === null || imageBase === undefined) {
        this.$message.error("图片数据为空");
      } else {
        this.axios
          .post("api/sso/faceLogin", formData, {
            headers: {
              "Content-Type": "multipart/form-data",
            },
          })
          .then((response) => {
            this.faceImgState = false;
            if (response.data.code == 200) {
              this.$message.success("登录成功");
              this.faceOption.thisVideo.srcObject.getTracks()[0].stop();
              Cookie.set("token", response.data.value.token);
              that.redirectUrl = response.data.value.redirectUrl;
              if (that.redirectUrl == "null" || that.redirectUrl == null) {
                this.$router.push("/home");
              } else {
                window.location.href = that.redirectUrl;
              }
            } else {
              this.$message.error(response.data.message);
            }
          })
          .catch((error) => {
            this.$message.error(error);
          });
        // this.$http.post("/api/sso/faceLogin", { imageBase }).then(
        //   (res) => {
        //     console.log(res);
        //     // this.faceImgState = false;
        //     // // 跳转首页
        //     // if (res.data.code === 200) {
        //     //   // 关闭摄像头
        //     //   this.faceOption.thisVideo.srcObject.getTracks()[0].stop();
        //     //   localStorage.setItem("face_token", res.data.token);
        //     //   localStorage.setItem("username", res.data.name);
        //     //   this.$message.success(res.data.msg);
        //     //   this.$router.push("/home");
        //     // }
        //     // if (res.data.code === 201) {
        //     //   this.$message.success(res.data.msg);
        //     // }
        //   },
        //   (onerror) => {
        //     this.faceImgState = false;
        //   }
        // );
      }
    },
    back() {
      this.faceOption.thisVideo.srcObject.getTracks()[0].stop();
      this.$router.push("/");
    },
  },
};
</script>
<style>
@import "./index.css";
</style>